package GUI_Element;

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.io.*;
import java.util.*;

import Kernel.Place;

public class ShapePlace extends Element {

	/* Its actual element */
	private Place p;
	
	/* GUI element and its scale */
	Ellipse2D.Float ellipse=null;
	final static float Radius = 15;

	/* Graph Structure Exponent */
	private AbstractSet<ShapeArc> PreArcSet = new HashSet<ShapeArc>();
	private AbstractSet<ShapeArc> PostArcSet = new HashSet<ShapeArc>();

	public ShapePlace(float x1, float y1) {
		type = Element.PLACE;
		p = new Place();
		this.x1=x1;
		this.y1=y1;
		ellipse=new Ellipse2D.Float(x1-Radius, y1-Radius, Radius*2, Radius*2);
		shape=ellipse;
	}
	
	public ShapePlace(ObjectInputStream ois) throws IOException {
		type = Element.PLACE;
		x1 = ois.readFloat();
		y1 = ois.readFloat();
		p = new Place(ois);
		ellipse=new Ellipse2D.Float(x1-Radius, y1-Radius, Radius*2, Radius*2);
		shape=ellipse;
	}

	public Element Clone() {
		return null;
	}

	public void draw(Graphics2D g) {
		if (isSelected)
			g.setColor(Element.SelectedColor);
		else
			g.setColor(Element.NormalColor);
		if (toBeDeleted)
			g.setColor(Element.DeleteColor);
		
		ellipse.x = x1-Radius;
		ellipse.y = y1-Radius;
		g.draw(shape);
		g.drawString("p" + p.PlaceIndex, x1-5, y1-Radius-5);
		DrawMarking(g);
/*		String marking = "" + p.GetMarking();
		if (p.GetMarking() > 0) {
			g.drawString(marking, x1-marking.length()*3, y1+4);
		}
*/	}
	
	private void DrawMarking(Graphics2D g) {
		long marking = p.GetMarking();
		if ( marking==0 )
			return;
		if ( marking==1 ){
			g.fillOval((int)x1-3, (int)y1-3, 6, 6);
			return;
		}
		if ( marking==2 ){
			g.fillOval((int)x1-7, (int)y1-3, 6, 6);
			g.fillOval((int)x1+2, (int)y1-3, 6, 6);
			return;
		}
		if ( marking==3 ){
			g.fillOval((int)x1-3, (int)y1-7, 6, 6);
			g.fillOval((int)x1-8, (int)y1, 6, 6);
			g.fillOval((int)x1+3, (int)y1, 6, 6);
			return;
		}
		String str = ""+marking;
		g.drawString(str, x1-str.length()*3, y1+4);
	}

	public Place GetActual() {
		return p;
	}
	
	public void SetPosition(float newx1, float newy1) {
		x1 = newx1;
		y1 = newy1;
	}
	
	public void IncrementToken() {
		p.FillToken(1);
	}
	
	public void AddPre(ShapeArc sa) {
		PreArcSet.add(sa);
	}
	public void AddPost(ShapeArc sa) {
		PostArcSet.add(sa);
	}
	public void RemovePre(ShapeArc sa) {
		PreArcSet.remove(sa);
	}
	public void RemovePost(ShapeArc sa) {
		PostArcSet.remove(sa);
	}
	public AbstractSet<ShapeArc> GetPreArcSet(){
		return this.PreArcSet;
	}
	public AbstractSet<ShapeArc> GetPostArcSet(){
		return this.PostArcSet;
	}
	
	public void Store(ObjectOutputStream oos) throws IOException{
		oos.writeFloat(x1);
		oos.writeFloat(y1);
		p.Store(oos);
	}
}
